﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class NewsManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserInfo"] == null || ((Entity.UserInfo)Session["UserInfo"]).UserType != "admin")
                {
                    Response.Redirect("Default.aspx");
                }

                DataTable dt = new DataTable();
                SQLHelper.getNews(out dt);
                gdvNews.DataSource = dt;
                gdvNews.DataKeyNames = new string[] { "Id" };
                gdvNews.DataBind();
                Session.Remove("NewsId");

            }
        }

        protected void gdvNews_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int i = Convert.ToInt32(gdvNews.DataKeys[e.RowIndex].Value);
            SQLHelper.deleteNewsById(i);
            Response.Redirect("NewsManage.aspx");
        }

        protected void gdvNews_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int i = Convert.ToInt32(gdvNews.DataKeys[e.NewEditIndex].Value);
            Session.Add("NewsId", i);
            Response.Redirect("NewsEdit.aspx");
        }

        protected void gdvNews_RowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[1].Visible = false;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Session.Remove("NewsId");
            Response.Redirect("NewsEdit.aspx");
        }
    }
}